/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include <hal_sdio.h>
/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_errno.h>

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

hal_sdio_t *hal_sdio_open(const char *name)
{
    return container_of(hal_open(name, 0), hal_sdio_t, parent);
}

int hal_sdio_close(hal_sdio_t *sdio)
{
    return hal_close((hal_dev_t *)sdio);
}

int hal_sdio_ioctl(hal_sdio_t *sdio, int cmd, ...)
{
    int ret;
    va_list valist;

    if (sdio == NULL) {
        return -EINVAL;
    }
    if (sdio->methods->ioctl == NULL) {
        return -ENOSYS;
    }
    va_start(valist, cmd);
    ret = sdio->methods->ioctl(sdio, cmd, valist);
    va_end(valist);

    return ret;
}

int hal_sdio_register(hal_sdio_t *sdio, const char *name, const hal_sdio_methods_t *methods,
                      uintptr_t data)
{
    int ret = 0;

    if (sdio == NULL || name == NULL || methods == NULL) {
        return -EINVAL;
    }

    sdio->parent.name = name;
    sdio->methods = methods;

    if ((ret = hal_register(&sdio->parent)) != 0) {
        return ret;
    }

    return ret;
}
